﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using JMBucknall.Containers;

namespace WindowsGame4
{
    class EventEmitter
    {
        static private EventEmitter eventEmitter = null;
        private EventManager eventManager;
        private PriorityQueue delayedMessages;

        private EventEmitter()
        {
            eventManager = EventManager.getInstance();
            delayedMessages = new PriorityQueue();
        }

        static public EventEmitter getInstance()
        {
            if (eventEmitter == null)
                eventEmitter = new EventEmitter();
            return eventEmitter;
        }

        public void reset()
        {
            delayedMessages = new PriorityQueue();
        }

        public void sendMessage(AIObject receiver, Message message)
        {
            receiver.HandleMessage(message);
        }

        public void registerMessage(double delay, String idEmitter, String idReceiver, Message.MessageType messageType)
        {
            try
            {
                // Get receiver object
                AIObject receiver = eventManager.getAIObject(idReceiver);
                // Create the message
                Message message = new Message(idEmitter, idReceiver, messageType, 0);
                if (delay == 0)
                {
                    // Send message to the receive
                    sendMessage(receiver, message);
                }
                else
                {
                    // Put the message in the queue
                    double currentTime = DateTime.Now.Ticks;
                    message.TimeSend = currentTime + delay;
                    delayedMessages.Enqueue(message, new ReversePriority(message.TimeSend));
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("petada: " + e);
                System.Console.ReadKey();
            }
        }

        public void sendDelayedMessages()
        {
            Boolean error = false;
            do
            {
                error = false;
                try
                {
                    // Get current time
                    double currentTime = DateTime.Now.Ticks;
                    Boolean stop = false;
                    // Check if there is any message in the queue
                    while (stop == false && delayedMessages.Count > 0)
                    {
                        Message message = (Message)delayedMessages.Dequeue();
                        // Check if we need to send it now
                        if (message.TimeSend <= currentTime)
                        {
                            // Get the receive
                            AIObject receiver = eventManager.getAIObject(message.IdReceiver);
                            sendMessage(receiver, message);
                        }
                        else
                        {
                            // Insert again in the queue
                            delayedMessages.Enqueue(message, new ReversePriority(message.TimeSend));
                            // Stop (the rest of the objects has higher time)
                            stop = true;
                        }
                    }
                }
                catch(Exception e)
                {
                    System.Console.WriteLine("error even manager: " +  e.ToString());
                    //error = true;
                }
            } while (error == true);
        }
    }
}